from django.contrib.auth import authenticate
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_simplejwt.tokens import RefreshToken

from user.models import User


# Create your views here.


class LoginView(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        if not all([username, password]):
            return Response({
                'code': 400,
                'msg': '参数不全'
            })
        user = authenticate(username=username, password=password)
        if not user:
            return Response({
                'code': 400,
                'msg': '用户名或密码错误'
            })
        refresh = RefreshToken.for_user(user)
        return Response({
            'code': 200,
            'msg': '登录成功',
        })

